#include "inimanager.h"
#include <QSettings>
#include <QDebug>
#include <QMutexLocker>

iniManager::iniManager(const QString &filePath, QObject *parent)
    : QObject(parent),
    m_filePath(filePath),
    m_settings(new QSettings(filePath, QSettings::IniFormat))  // 使用智能指针管理QSettings
{
    // 无需显式调用 initSettings()，直接在构造函数中初始化 QSettings
}

iniManager::~iniManager()
{
    // 智能指针会自动清理内存，无需手动删除 m_settings
}

// 读取指定组和键的值
QVariant iniManager::readValue(const QString &group, const QString &key, const QVariant &defaultValue) const
{
    QMutexLocker locker(&m_mutex);  // 确保线程安全
    m_settings->beginGroup(group);
    QVariant value = m_settings->value(key, defaultValue);
    m_settings->endGroup();
    return value;
}

// 写入指定组和键的值
bool iniManager::writeValue(const QString &group, const QString &key, const QVariant &value)
{
    QMutexLocker locker(&m_mutex);  // 确保线程安全
    m_settings->beginGroup(group);
    m_settings->setValue(key, value);
    m_settings->endGroup();
    return true;
}

// 添加新的组和键值对
bool iniManager::addGroup(const QString &group, const QVariantMap &keyValues)
{
    return writeValues(group, keyValues);
}

// 删除指定的组
bool iniManager::removeGroup(const QString &group)
{
    QMutexLocker locker(&m_mutex);
    m_settings->beginGroup(group);
    m_settings->remove("");  // 删除组中的所有键值对
    m_settings->endGroup();
    return true;
}

// 删除指定组中的某个键
bool iniManager::removeValue(const QString &group, const QString &key)
{
    QMutexLocker locker(&m_mutex);
    m_settings->beginGroup(group);
    m_settings->remove(key);
    m_settings->endGroup();
    return true;
}

// 获取所有的组名
QStringList iniManager::allGroups() const
{
    QMutexLocker locker(&m_mutex);
    return m_settings->childGroups();
}

// 获取指定组中的所有键
QStringList iniManager::allKeys(const QString &group) const
{
    QMutexLocker locker(&m_mutex);
    m_settings->beginGroup(group);
    QStringList keys = m_settings->childKeys();
    m_settings->endGroup();
    return keys;
}

// 检查是否存在指定键和值的匹配
bool iniManager::isExists(const QString &key, const QVariant &value) const
{
    QMutexLocker locker(&m_mutex);
    QStringList groups = allGroups();
    for (const QString &group : groups) {
        if (readValue(group, key) == value) {
            return true;
        }
    }
    return false;
}

// 获取包含指定键和值的组
QString iniManager::getGroup(const QString &key, const QVariant &value) const
{
    QMutexLocker locker(&m_mutex);
    QStringList groups = allGroups();
    for (const QString &group : groups) {
        if (readValue(group, key) == value) {
            return group;
        }
    }
    return QString();
}

// 写入多个键值对到指定组
bool iniManager::writeValues(const QString &group, const QVariantMap &keyValues)
{
    QMutexLocker locker(&m_mutex);
    m_settings->beginGroup(group);
    for (auto it = keyValues.constBegin(); it != keyValues.constEnd(); ++it) {
        m_settings->setValue(it.key(), it.value());
    }
    m_settings->endGroup();
    return true;
}

// 同步设置到存储
bool iniManager::sync()
{
    QMutexLocker locker(&m_mutex);
    m_settings->sync();
    return (m_settings->status() == QSettings::NoError);
}
